﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CloudFranceTriple.Models;

namespace CloudFranceTriple.WebRole.Areas.Portail.Controllers
{
    [CloudFranceTriple.WebRole.Filters.InitializeSimpleMembership]
    public class ShoutBoxController : Controller
    {
        private cloudfrAboq2RsDMEntities db = new cloudfrAboq2RsDMEntities();

        //
        // GET: /Portail/ShoutBox/ShoutBoxMessageAdd
        [ChildActionOnly]
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminShoutBox")]
        public ActionResult ShoutBoxMessageAdd()
        {
            return PartialView("_ShoutBoxMessageAdd");
        }

        //
        // POST: /Portail/ShoutBox/ShoutBoxMessageAdd

        [HttpPost]
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminShoutBox")]
        public ActionResult ShoutBoxMessageAdd(ShoutBoxSet shoutboxMsg)
        {
            if (shoutboxMsg != null && shoutboxMsg.MessageShoutBox != null && shoutboxMsg.MessageShoutBox != String.Empty && ModelState.IsValid)
            {
                if (db.ShoutBoxSet.Where(msg => (msg.MessageShoutBox == shoutboxMsg.MessageShoutBox) && (msg.Id == db.ShoutBoxSet.Max(msg2 => msg2.Id))).Count() == 0)
                {
                    shoutboxMsg.CreationDateShoutBox = System.DateTime.UtcNow;
                    shoutboxMsg.UserNameAuthorShoutBox = User.Identity.Name;
                    db.ShoutBoxSet.Add(shoutboxMsg);
                    db.SaveChanges();
                }
            }
            return Redirect(Request.UrlReferrer.PathAndQuery.ToString());
        }

        //
        // GET: /Portail/ShoutBox/ShoutBoxMessageAdd
        [ChildActionOnly]
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminShoutBox")]
        public ActionResult ShoutBoxMessageList()
        {
            return PartialView("_ShoutBoxMessageList", db.ShoutBoxSet.OrderByDescending(shout => shout.CreationDateShoutBox).ToList());
        }

        //
        // GET: /Portail/ShoutBox/
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminShoutBox")]
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Portail/ShoutBox/Details/5

        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminShoutBox")]
        public ActionResult Details(int id = 0)
        {
            ShoutBoxSet shoutboxset = db.ShoutBoxSet.Find(id);
            if (shoutboxset == null)
            {
                return HttpNotFound();
            }
            return View(shoutboxset);
        }

        //
        // GET: /Portail/ShoutBox/Edit/5

        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminShoutBox")]
        public ActionResult Edit(int id = 0)
        {
            ShoutBoxSet shoutboxset = db.ShoutBoxSet.Find(id);
            if (shoutboxset == null)
            {
                return HttpNotFound();
            }
            return View(shoutboxset);
        }

        //
        // POST: /Portail/ShoutBox/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminShoutBox")]
        public ActionResult Edit(ShoutBoxSet shoutboxset)
        {
            if (ModelState.IsValid)
            {
                shoutboxset.CreationDateShoutBox = DateTime.UtcNow;
                db.Entry(shoutboxset).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(shoutboxset);
        }

        //
        // GET: /Portail/ShoutBox/Delete/5

        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminShoutBox")]
        public ActionResult Delete(int id = 0)
        {
            ShoutBoxSet shoutboxset = db.ShoutBoxSet.Find(id);
            if (shoutboxset == null)
            {
                return HttpNotFound();
            }
            return View(shoutboxset);
        }

        //
        // POST: /Portail/ShoutBox/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminShoutBox")]
        public ActionResult DeleteConfirmed(int id)
        {
            ShoutBoxSet shoutboxset = db.ShoutBoxSet.Find(id);
            db.ShoutBoxSet.Remove(shoutboxset);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}